package com.starbaba.mredis.view;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.message.BasicNameValuePair;

import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.model.utils.HttpConnectionUtils;

public class MredisAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1629639856220872834L;

	/**
	 * 编辑app记录
	 * 
	 * @return
	 * @throws IOException
	 */
	public void delKey() throws IOException {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String key = (String) paramsMap.get("del_key");
		String connection = HttpConnectionUtils.connection(Constant.COMMON_HOST + "/mredis/del/del?key=" + key, HttpConnectionUtils.POST);
		sendJsonMessage(connection);
	}

	public void getKey() throws IOException {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String key = (String) paramsMap.get("get_key");
		ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("key", key));
		String connection = HttpConnectionUtils.connection(Constant.COMMON_HOST + "/mredis/get/get", HttpConnectionUtils.POST, list);
		sendJsonMessage(connection);
	}

	public void modSet() throws IOException {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String key = (String) paramsMap.get("mod_key");
		String value = (String) paramsMap.get("mod_value");
		ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("key", key));
		list.add(new BasicNameValuePair("value", value));
		String connection = HttpConnectionUtils.connection(Constant.COMMON_HOST + "/mredis/oper/set", HttpConnectionUtils.POST, list);
		sendJsonMessage(connection);
	}

	public void modHset() throws IOException {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String name = (String) paramsMap.get("mod_hset_name");
		String key = (String) paramsMap.get("mod_hset_key");
		String value = (String) paramsMap.get("mod_hset_value");
		ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("key", key));
		list.add(new BasicNameValuePair("map", name));
		list.add(new BasicNameValuePair("value", value));
		String connection = HttpConnectionUtils.connection(Constant.COMMON_HOST + "/mredis/oper/hset", HttpConnectionUtils.POST, list);
		sendJsonMessage(connection);
	}

	public void delHset() throws IOException {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String name = (String) paramsMap.get("del_hset_name");
		String key = (String) paramsMap.get("del_hset_key");
		ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("key", key));
		list.add(new BasicNameValuePair("name", name));
		String connection = HttpConnectionUtils.connection(Constant.COMMON_HOST + "/mredis/del/hdel", HttpConnectionUtils.POST, list);
		sendJsonMessage(connection);
	}

	public void modLList() throws IOException {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String key = (String) paramsMap.get("mod_llist_key");
		String lpush = (String) paramsMap.get("mod_llist_lpush");
		ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("key", key));
		list.add(new BasicNameValuePair("para0", lpush));
		String connection = HttpConnectionUtils.connection(Constant.COMMON_HOST + "/mredis/oper/lpush", HttpConnectionUtils.POST, list);
		sendJsonMessage(connection);
	}

	public void modRList() throws IOException {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String key = (String) paramsMap.get("mod_rlist_key");
		String rpush = (String) paramsMap.get("mod_rlist_rpush");
		ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("key", key));
		list.add(new BasicNameValuePair("para0", rpush));
		String connection = HttpConnectionUtils.connection(Constant.COMMON_HOST + "/mredis/oper/rpush", HttpConnectionUtils.POST, list);
		sendJsonMessage(connection);
	}

	public void getList() throws IOException {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String key = (String) paramsMap.get("get_list_key");
		ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("key", key));
		String connection = HttpConnectionUtils.connection(Constant.COMMON_HOST + "/mredis/get/lrange", HttpConnectionUtils.POST, list);
		sendJsonMessage(connection);
	}
}
